package cards.controller;

import cards.dao.UserDao;
import cards.entity.AttendedCardActivity;
import cards.entity.User;
import cards.service.AttendedCardActivityService;
import cards.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class AttendedCardActivitiesController {

    @Autowired
    AttendedCardActivityService attendedCardActivityService;

    @Autowired
    UserDao userDao;
    @Autowired
    HostHolder localHost;

    @RequestMapping(path="/{usertype}/attended", method= RequestMethod.GET)
    public String displayAttendedCardActivities(@PathVariable String usertype, Model model) {

        User user = localHost.getUser();
        // 有 bug 要修改
        List<AttendedCardActivity> pageList = attendedCardActivityService.findAttendedCardActivityByUsernameOrderedById(user.getUsername(), usertype);

        int cardActivitiesTotalNum = pageList.size();
        model.addAttribute("user", user);
        model.addAttribute("topics", pageList);
        model.addAttribute("topicsTotalNum", cardActivitiesTotalNum);
        model.addAttribute("header", "已参与活动");
        model.addAttribute("userDao", userDao);
        return "attended";
    }
}
